package es.uclm.itsue.SQLiteContentProvider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.database.sqlite.SQLiteDatabase;

public class BibliotecaSQLiteProvider extends ContentProvider {

	private static final String authority =	"es.uclm.itsue.sqlitecontentprovider.bibliotecasqliteprovider";
	private static final String providerName = "content://"+authority;
	public static final Uri CONTENT_URI = Uri.parse(providerName);
	
	public final static String _ID = "_id";
	public final static String NOMBRE = "nombre";
	public final static String DNI = "dni";
	public final static String TITULO = "titulo";
	public final static String AUTOR = "autor";	
	
    private static final int TABLA_LECTORES = 0;
    private static final int TABLA_LIBROS = 1;      
    private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    static {
       uriMatcher.addURI(authority, "lectores", TABLA_LECTORES);
       uriMatcher.addURI(authority, "libros", TABLA_LIBROS);      
    }
	
	private BibliotecaSQLiteHelper bibliotecaDBHelper;
	SQLiteDatabase bibliotecaDB;

	@Override
	public boolean onCreate() {
		bibliotecaDBHelper = new BibliotecaSQLiteHelper(getContext(), "biblioteca.s3db", null, 2);
        bibliotecaDB = bibliotecaDBHelper.getWritableDatabase();
        return true;
        
	}
	
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		Cursor resultCursor;
		
		switch (uriMatcher.match(uri)) {
		case TABLA_LECTORES:
			resultCursor = bibliotecaDB.query("lectores", projection,
					selection, selectionArgs, null, null, sortOrder, null);
			break;
        case TABLA_LIBROS:                        	
			resultCursor = bibliotecaDB.query("libros", projection,
					selection, selectionArgs, null, null, sortOrder, null);
        	break;
        default:
           throw new IllegalArgumentException("Unsupported URI: " + uri);		
		}		
		
		return resultCursor;		
	}
	
	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long res; 				
		
		switch (uriMatcher.match(uri)) {
		case TABLA_LECTORES:
			res = bibliotecaDB.insert("lectores", "", values);
			if (res<=0)
		    	throw new SQLException("Failed to insert row into " + uri);
			break;
        case TABLA_LIBROS:                        	
			res = bibliotecaDB.insert("libros", "", values);
			if (res<=0)
		    	throw new SQLException("Failed to insert row into " + uri);        	
        	break;
        default:
           throw new IllegalArgumentException("Unsupported URI: " + uri);		
		}
		
		return null;
	}
	
	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		return 0;
	}

}



